<template>
	<!-- 购物车提交，确认订单信息页面 -->
	<view>
		<view v-for="item in shop">
			<!-- 图文卡片模式 -->
			<uni-card>
				<text class="small">商品名：{{item.name}}</text>
				<br />
				<text class="small">{{item.price}}元/件x{{item.number}}件=<text
						style="font-size: large;">{{item.price*item.number}}</text>元</text>
			</uni-card>
		</view>
		<view>共计：{{allPrice}}</view>
		<view>收件人：{{username}}</view>
		<view>收货地址：{{address}}</view>
		<view>联系电话：{{phone}}</view>
		<view class="position-sticky fixed-bottom">
			<button :plain="false" @click="submit()">结算</button>
			<button :plain="false" @click="back()">返回</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: uni.getStorageSync("token"),
				/* 同步获取用户名 */
				address: '',
				phone: '',
				shop: [{
					name: '',
					imgSrc: [{}],
					id: 0,
					weight: 0,
					issueDate: '',
					price: 0,
					number: 0,
				}],
			}
		},
		methods: {
			submit() {
				/* 结算*/
				let order={};
				order.username=this.username;
				order.date=this.currTime();
				order.all=this.allPrice;
				order.orderItems=this.shop;
				uni.request({
					url: 'http://localhost:8081/shopCar/submitShopping',
					method:'POST',
					data:order,
					success: (res) => {
						if (res.data.result) {
							/* 扣钱，支付成功 */
							/* 创建订单 */
							uni.navigateTo({
								url:'../user/userOrder'
							})
						} else {
							/* 没钱 */
							uni.navigateTo({
								url: '../user/addMoney',
							})
						}
					},
				})
			},
			currTime() {
				let date = new Date()
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				let hour = date.getHours();
				let min = date.getMinutes();
				let sec = date.getSeconds();
				return year + "-" + month + "-" + day + "-" + hour + ":" + min + ":" + sec;
			},
			back() {
				uni.navigateBack({

				})
			},
			onLoadShopCar() {
				uni.request({
					url: 'http://localhost:8081/shopCar/selectShopCar?username=' + this.username,
					success: (res) => {
						this.shop = res.data.shop;
					},
				})
			},
			onLoadUserInfo() {
				uni.request({
					url: 'http://localhost:8081/user/selectOneUserByUsername?username=' + this.username,
					success: (res) => {
						if (res.data.result) {
							this.address = res.data.user.address; //获取地址
							this.phone = res.data.user.phone; //获取电话号码
						}
					},
				})
			}
		},
		computed: {
			allPrice() {
				let all = 0;
				for (let i = 0; i < this.shop.length; i++) {
					all = all + this.shop[i].number * this.shop[i].price;
				}
				return all;
			}
		},
		onLoad() {
			this.onLoadShopCar();
			this.onLoadUserInfo();
		}
	}
</script>

<style>
	.small {
		font-size: small;
	}

	.position-sticky {
		position: -webkit-sticky !important;
		position: sticky !important;
	}

	.fixed-bottom {
		position: fixed;
		right: 0;
		bottom: calc(var(--window-bottom) + 3px);
		left: 0;
		z-index: 1030;
		margin-bottom: 6;
		text-align: right;
		margin-right: 30rpx;
	}
</style>
